import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

// import { h } from 'vue';
import { DictEnum } from '@vben/constants';
// import { VbenIcon } from '@vben/icons';
import { getPopupContainer } from '@vben/utils';

// import { Tag } from 'ant-design-vue';
// import { z } from '#/adapter/form';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'RegisterTrackCategorySelect',
    fieldName: 'categoryId',
    label: '来源分类',
  },
  {
    component: 'Input',
    fieldName: 'sourceKey',
    label: '来源KEY',
  },
  {
    component: 'Input',
    fieldName: 'sourceName',
    label: '来源名称',
  },
  // {
  //   component: 'Input',
  //   fieldName: 'oldUrl',
  //   label: '旧链接',
  // },
  // {
  //   component: 'Input',
  //   fieldName: 'newUrl',
  //   label: '新链接',
  // },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.REGISTER_SOURCE_TRACK_URL_TYPE, true),
    },
    fieldName: 'urlType',
    label: '链接类型',
  },
  {
    component: 'Select',
    fieldName: 'number',
    label: '届数',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.EXHIBITION_NUMBER, true),
    },
  },
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'status',
    label: '状态',
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '来源名称',
    field: 'sourceName',
    minWidth: 300,
  },
  {
    title: '来源KEY',
    field: 'sourceKey',
    minWidth: 200,
  },
  {
    title: '来源分类',
    field: 'categoryName',
    // slots: {
    //   default: ({ row }) => {
    //     return renderDict(row.typeId, DictEnum.REGISTER_SOURCE_TRACK_TYPE);
    //   },
    // },
    width: 120,
  },
  {
    title: '追踪链接',
    field: 'newUrl',
    width: 200,
  },
  {
    title: '链接类型',
    field: 'urlType',
    slots: {
      default: ({ row }) => {
        return renderDict(row.urlType, DictEnum.REGISTER_SOURCE_TRACK_URL_TYPE);
      },
    },
    width: 140,
  },
  {
    title: '届数',
    field: 'number',
    width: 80,
  },
  // {
  //   title: '状态',
  //   field: 'status',
  // },
  {
    title: '备注',
    field: 'remark',
    visible: false,
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
    width: 140,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: '主键',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '来源分类',
    fieldName: 'categoryId',
    component: 'RegisterTrackCategorySelect',
    help: '对观众数据来源渠道进行分类',
    rules: 'selectRequired',
  },
  {
    label: '届数',
    fieldName: 'number',
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.EXHIBITION_NUMBER, true),
    },
    rules: 'selectRequired',
  },
  // {
  //   label: '来源KEY',
  //   fieldName: 'sourceKey',
  //   component: 'Input',
  //   extra:
  //     'YYYYMMdd分类-渠道-名称 (YYYYMMdd：yyyy为年份，MM为月份，dd为日期；使用英文单词或拼音首字母，统一大写)',
  //   help: '定义一个追踪标识，将会放置将链接参数中，当用户提交表单如果命中这个标识则会被统计。',
  //   rules: 'required',
  // },
  // {
  //   label: '来源名称',
  //   fieldName: 'sourceName',
  //   component: 'Input',
  //   help: '内部标题，用于描述来源KEY的含义',
  //   rules: 'required',
  // },
  {
    label: '来源用途',
    fieldName: 'purpose',
    component: 'Input',
    help: '将自动生成来源名称和KEY，名称格式为「YYYYMMdd-用途」；KEY格式为「时间戳+追踪分类KEY」',
    rules: 'required',
  },
  {
    label: '链接类型',
    fieldName: 'urlType',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.REGISTER_SOURCE_TRACK_URL_TYPE, true),
      optionType: 'button',
    },
    rules: 'selectRequired',
  },
  {
    label: 'APPID',
    fieldName: 'appId',
    component: 'RadioGroup',
    dependencies: {
      if: (values) => values?.urlType === 2,
      triggerFields: ['urlType'],
    },
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.WECHAT_MINIPROGRAM_LIST),
      optionType: 'button',
    },
    help: '微信小程序APPID',
    rules: 'required',
  },
  {
    label: '原始链接',
    fieldName: 'oldUrl',
    component: 'Input',
    help: '需追踪转化且不含参数的原始链接',
    dependencies: {
      if: (values) => values?.urlType === 1,
      triggerFields: ['urlType'],
    },
    rules: 'required',
  },
  {
    label: '原始链接',
    fieldName: 'oldUrl',
    component: 'AutoComplete',
    help: '需追踪转化且不含参数的原始链接',
    dependencies: {
      if: (values) => values?.urlType === 2,
      triggerFields: ['urlType'],
    },
    componentProps: {
      placeholder: '自定义输入或选择',
      options: [],
    },
    rules: 'required',
  },
  {
    label: '备注',
    fieldName: 'remark',
    component: 'Textarea',
  },
];

export const detailsSchema: DetailsModalSchemaGetter = () => [
  {
    label: '来源名称',
    fieldName: 'sourceName',
    copy: true,
  },
  {
    label: '来源KEY',
    fieldName: 'sourceKey',
    copy: true,
  },
  {
    label: '来源分类',
    fieldName: 'categoryName',
  },
  {
    label: '原始链接',
    fieldName: 'oldUrl',
    copy: true,
  },
  {
    label: '追踪链接',
    fieldName: 'newUrl',
    copy: true,
  },
  {
    label: '链接类型',
    fieldName: 'urlType',
    dictEnum: DictEnum.REGISTER_SOURCE_TRACK_URL_TYPE,
  },
  {
    label: 'APPID',
    fieldName: 'appId',
    dictEnum: DictEnum.WECHAT_MINIPROGRAM_LIST,
    dependencies: {
      if: (values) => values?.urlType === 2,
    },
  },
  {
    label: '届数',
    fieldName: 'number',
  },
  {
    label: '备注',
    fieldName: 'remark',
    textWrap: true,
  },
];
